package com.eleadmin.gps.utils;

import com.eleadmin.damon.utils.utils.CacheUtils;
import com.eleadmin.damon.utils.utils.DateUtil;
import com.eleadmin.damon.utils.utils.StringUtil;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.gps.dao.CarPosition;
import com.eleadmin.gps.param.CarpositionParam;
import com.openapi.sdk.service.DataExchangeService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @author Damon
 * @create 2020-11-12 13:46
 */
@Component
public class ZjxlGps {
//    //测试账号
    public static final String USER_TEST = "cc68c016-d3ae-447a-841a-32c1762633be";
    public static final String PWD_TEST = "4gr0EJJo0xg82o448v5E73904W5728";
    public static final String CID_TEST = "53777328-0436-41b2-981d-642b2bf2abd1";
    public static final String SRT_TEST = "bb5226cf-32fb-4474-8926-a3840eac1312";
    public static final String URL_TEST = "https://openapi-test.sinoiov.cn";//_TEST

//正式账号
    public static String USER = "a0cb07cc-7134-4561-8bce-9855639b6f47";
    public static String PWD = "1672579i11M2B7Xsl54jwe619s158Y";
    public static String CID = "87f010f2-7a8d-4ef5-bd1a-899039d20850";
    public static String SRT = "0c3dcb98-6778-46d1-9661-e1b0c12cda7a";
    public static String URL = "https://zhiyunopenapi.95155.com";//_PROD

    public static void setStr(String runwithmodel){
        if (!runwithmodel.equals("prod")) {
            USER = USER_TEST;
            PWD = PWD_TEST;
            CID = CID_TEST;
            SRT = SRT_TEST;
            URL = URL_TEST;
        }
    }

//    public static void main(String[] args) throws Exception {
//        ZjxlGps.vLastLocationV3();
//        ZjxlGps.vLastLocationV3();
//        ZjxlGps.vLastLocationV3();
//        ZjxlGps.vLastLocationV4();
//        ZjxlGps.vLastLocationV4();
//        ZjxlGps.vLastLocationV4("浙B69916_2,浙B0E778_2,浙BJ8510_2,浙L17599_2,浙B87G17_2,浙CJ3126_2");
//
//        "lon" : "72705747", "lat" : "17131053"
//
//    }

    //    https://blog.csdn.net/nmjhehe/article/details/83038086
//    RestTemplete 忽略安全证书
    public void login2() throws Exception {
        String url = URL+"/save/apis/login/";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("user", "cc68c016-d3ae-447a-841a-32c1762633be");
        map.add("pwd", "8L31Xs6ZSeWj5nw5tS4A4dk8jM0bv2");
        map.add("cid", "53777328-0436-41b2-981d-642b2bf2abd1");
        map.add("srt", "f0c60cbc-a1a2-4bde-80fd-82da13b94a38");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ZjxlGps l = new ZjxlGps();
        String res = new RestTemplate(l.generateHttpRequestFactory()).postForObject(url, request, String.class);
        System.out.println(res);

//        String data = "{\"user\": \"cc68c016-d3ae-447a-841a-32c1762633be\", " +
//                "\"pwd\": \"8L31Xs6ZSeWj5nw5tS4A4dk8jM0bv2\", " +
//                "\"cid\": \"53777328-0436-41b2-981d-642b2bf2abd1\", " +
//                "\"srt\": \"f0c60cbc-a1a2-4bde-80fd-82da13b94a38\"}";
//        String post = HttpsUtils.post(url, data);
//        String ticket = JSONObject.fromObject(post).getString("ticket");
    }

    //返回:{"result":"23fbde54-cb1a-4c87-9c5a-c8a840efd6ce","status":1001}
    public static String login() {
        try {
            Map<String, String> map = new HashMap<String, String>(4);
            map.put("user", USER);
            map.put("pwd", PWD);
            map.put("cid", CID);
            map.put("srt", SRT);

            String url = URL+"/save/apis/login/";
            // 设置 http 读写超时
            DataExchangeService des = new DataExchangeService(5000, 8000);
            System.out.println("请求地址:"+url);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);

            String ticket = JSONObject.fromObject(res).getString("result");
            System.out.println("返回ticket:"+ ticket);
            return ticket;
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
            return null;
        }
    }

    public static String getToken() {
        String token = CacheUtils.timedCacheGpsToken.get("gpsToken");
        if (Utils.isNotEmpty(token)&& !token.equals("null")) {
        } else  {
            token = login();
            System.out.println("create new token!!");
            CacheUtils.timedCacheGpsToken.put("gpsToken", token);
        }
        return token;
    }


    /** 一
     * 实时点位 但是要钱
     * 5.1.1.3.1. 运输节点服务
     * */
    public static JSONArray transTimeManageV3(CarpositionParam carpositionParam, String runwithmodel) {
        try {
            setStr(runwithmodel);
            Map<String, String> map = new HashMap<String, String>(3);
            map.put("token", getToken());
            map.put("cid", CID);
            map.put("srt", SRT);
            //车牌号_车牌颜色，多个车辆以英文逗号分隔（最多 100 台车，颜色：1 蓝色、2 黄色、3黄绿）
            // 京A78601_1,京A78602_2, 京A78603_3
            map.put("vnos", carpositionParam.getVnos());
            String url = URL+"/save/apis/transTimeManageV3";
            DataExchangeService des = new DataExchangeService(5000, 8000);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);
            JSONObject obj = JSONObject.fromObject(res);
            JSONObject obj3 = obj.getJSONObject("result");
            JSONObject obj4 = obj3.getJSONObject("firstVcl");

            JSONObject firstVcl = obj3.getJSONObject("firstVcl");
            JSONArray others = obj3.getJSONArray("others");
            JSONArray collect = new JSONArray();
            collect.addAll(others);
            collect.add(firstVcl);
            return collect;
//            if(Utils.isNotEmpty(obj4)){
//                try {
//                    CarpositionResult carPosition = new CarpositionResult();
//                    Float lon = Float.valueOf(obj4.getString("lon"))/600000;
//                    carPosition.setLon(lon.toString());
//                    Float lat = Float.valueOf(obj4.getString("lat"))/600000;
//                    carPosition.setLat(lat.toString());
//                    carPosition.setUtc(DateUtil.timeStamp2Date(obj4.getString("utc"), ""));
//                    carPosition.setSpd(obj4.getString("spd"));
//                    carPosition.setAdr(obj4.getString("adr"));
//                    carPosition.setDrc(obj4.getString("drc"));
//                    return carPosition;
//                } catch (Exception e) {
//                }
//            }
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
        }
        return null;
    }

    /** 一
     * 轨迹 所有车辆 但是要钱
     * 5.1.4.3.1. 运输中转服务
     * 输入参数
     * {"vList":"[{"sort":1,"vclN":"冀 LW0366","vco":2,"qryBtm":"2021-07-28 18:09:30","qryEtm":"202
     * 1-07-28 19:20:30"},{"sort":2,"vclN":"冀 LW0377","vco":2,"qryBtm":"2021-07-28 18:40:30","qr
     * yEtm":"2021-07-28 20:50:30"},{"sort":3,"vclN":"冀 LW0388","vco":2,"qryBtm":"2021-07-28 20: 35:30","qryEtm":"2021-07-28 22:09:30"}]","sign":"XXX","cid":"XXX"}
     * */
    public static JSONArray goodsTransferTrack(CarpositionParam carpositionParam, String runwithmodel) {
        try {
            setStr(runwithmodel);
            Map<String, String> map = new HashMap<String, String>(3);
            map.put("token", getToken());
            map.put("cid", CID);
            map.put("srt", SRT);
            JSONArray ja2 = new JSONArray();
            JSONObject jb2 = new JSONObject();
            jb2.put("sort", 1);
            //车牌号 陕 YH0009
            jb2.put("vclN", carpositionParam.getVclN());
            jb2.put("vco", 2);//1 蓝色、2 黄色、3 黄绿色
            jb2.put("qryBtm", carpositionParam.getQryBtm());
            jb2.put("qryEtm", carpositionParam.getQryEtm());
            ja2.add(jb2);
            map.put("vList", ja2.toString());
            String url = URL+"/save/apis/goodsTransferTrack";
            DataExchangeService des = new DataExchangeService(5000, 8000);
//            System.out.println("请求地址:"+url);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);
//            System.out.println("vclNs+billIds:"+ vclNs+"   "+billIds);
//            System.out.println("返回res:"+ res);
            JSONObject obj = JSONObject.fromObject(res);
            JSONObject obj3 = obj.getJSONObject("result");
            JSONArray ja = obj3.getJSONArray("routes");
            JSONArray collect = new JSONArray();
            collect.addAll(ja);
            return collect;
//            List<CarpositionResult> carPositions = new ArrayList<>();
//
//            if (ja != null && ja.size() > 0) {
//                for (int i = 0; i < ja.size(); i++) {
//                    JSONObject obj2 = ja.getJSONObject(i);
//                    if(StringUtil.isBlank(obj2.getString("lon"),obj2.getString("lat"),obj2.getString("gtm"))){
//                        continue;
//                    }
//                    try {
//                        CarpositionResult carPosition = new CarpositionResult();
//                        Float lon = Float.valueOf(obj2.getString("lon"))/600000;
//                        carPosition.setLon(lon.toString());
//                        Float lat = Float.valueOf(obj2.getString("lat"))/600000;
//                        carPosition.setLat(lat.toString());
//                        carPosition.setGtm(obj2.getString("gtm"));
//                        carPosition.setSpd(obj2.getString("spd"));
//                        carPosition.setMlg(obj2.getString("mlg"));
//                        carPosition.setHgt(obj2.getString("hgt"));
////                        carPosition.setUtc(DateUtil.timeStamp2Date(obj2.getString("utc"), ""));
//                        carPositions.add(carPosition);
//                    } catch (Exception e) {
//                    }
//                }
//            }
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
        }
        return null;
    }



    /** 一、 车辆最新位置查询（车牌号）接口
     * 本接口提供指定车牌号的车辆最新位置查询。
     * */
    public static CarPosition vLastLocationV3(String vno, String runwithmodel) {
        try {
            setStr(runwithmodel);
            Map<String, String> map = new HashMap<String, String>(3);
            map.put("token", getToken());
            map.put("cid", CID);
            map.put("srt", SRT);
            map.put("vclN", vno);
            map.put("timeNearby", "30");
            String url = URL+"/save/apis/vLastLocationV3";
            DataExchangeService des = new DataExchangeService(5000, 8000);
//            System.out.println("请求地址:"+url);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);
            JSONObject obj = JSONObject.fromObject(res);
            JSONObject obj2 = obj.getJSONObject("result");
            System.out.println("obj2:::"+obj2);
            CarPosition carPosition = new CarPosition();
//            carPosition.setId(ObjectId.get());
            carPosition.setVno(vno.substring(0,vno.length()-3));
//            carPosition.setOriginalLon(obj2.getString("lon"));
//            carPosition.setOriginalLat(obj2.getString("lat"));
            Float lon = Float.valueOf(obj2.getString("lon"))/600000;
            carPosition.setLon(lon.toString());
            Float lat = Float.valueOf(obj2.getString("lat"))/600000;
            carPosition.setLat(lat.toString());
            carPosition.setAdr(obj2.getString("adr"));
            carPosition.setSpd(obj2.getString("spd"));
            carPosition.setProvince(obj2.getString("province"));
            carPosition.setCity(obj2.getString("city"));
            carPosition.setCountry(obj2.getString("country"));
            carPosition.setDrc(obj2.getString("drc"));
            carPosition.setUtc(DateUtil.timeStamp2Date(obj2.getString("utc"), ""));

//            System.out.println("返回:"+ res);
            return carPosition;
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
        }
        return null;
    }
    /** 一、 车辆最新位置查询（车牌号）接口
     * 本接口提供指定车牌号的车辆最新位置查询。
     * */
    public static JsonResult vLastLocationV4_origin(String vclNs, String runwithmodel) {
        try {
            setStr(runwithmodel);
            Map<String, String> map = new HashMap<String, String>(3);
            map.put("token", getToken());
            map.put("cid", CID);
            map.put("srt", SRT);
            map.put("vclNs", vclNs);
            map.put("timeNearby", "30");
            String url = URL+"/save/apis/vLastLocationMultiV4";
            DataExchangeService des = new DataExchangeService(5000, 8000);
//            System.out.println("请求地址:"+url);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);
//            System.out.println("vclNs+billIds:"+ vclNs);
//            System.out.println("返回res:"+ res);
            JSONObject obj = JSONObject.fromObject(res);
            JSONArray ja = obj.getJSONArray("result");

            List<CarPosition> carPositions = new ArrayList<>();
            if (ja != null && ja.size() > 0) {
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject obj2 = ja.getJSONObject(i);
                    if (StringUtil.isBlank(obj2.getString("lon"), obj2.getString("lat"), obj2.getString("utc"))) {
                        continue;
                    }
                    try {
                        CarPosition carPosition = new CarPosition();
//                        carPosition.setId(ObjectId.get());
                        carPosition.setVno(obj2.getString("vno"));
                        Float lon = Float.valueOf(obj2.getString("lon")) / 600000;
                        carPosition.setLon(lon.toString());
                        Float lat = Float.valueOf(obj2.getString("lat")) / 600000;
                        carPosition.setLat(lat.toString());
                        carPosition.setVco(obj2.getString("vco"));
                        carPosition.setAdr(obj2.getString("adr"));
                        carPosition.setSpd(obj2.getString("spd"));
                        carPosition.setProvince(obj2.getString("province"));
                        carPosition.setCity(obj2.getString("city"));
                        carPosition.setCountry(obj2.getString("country"));
                        carPosition.setDrc(obj2.getString("drc"));
                        carPosition.setUtc(DateUtil.timeStamp2Date(obj2.getString("utc"), ""));
                        carPosition.setCreateDate(DateUtil.getCurrentDate());
                        carPositions.add(carPosition);
                    } catch (Exception e) {

                    }
                }
            }
            return JsonResult.ok().setData(ja);
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
        }
        return null;
    }
    /** 一、 车辆最新位置查询（车牌号）接口
     * 本接口提供指定车牌号的车辆最新位置查询。
     * */
    public static List<CarPosition> vLastLocationV4(String vclNs, String billIds, String runwithmodel) {
//        if (!JKXStartupRunner.runwithmodel2.equals("dev")) { return null;}
        try {
            setStr(runwithmodel);
            Map<String, String> map = new HashMap<String, String>(3);
            map.put("token", getToken());
            map.put("cid", CID);
            map.put("srt", SRT);
            map.put("vclNs", vclNs);
            map.put("timeNearby", "30");
            String url = URL+"/save/apis/vLastLocationMultiV4";
            DataExchangeService des = new DataExchangeService(5000, 8000);
//            System.out.println("请求地址:"+url);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);
//            System.out.println("vclNs+billIds:"+ vclNs+"   "+billIds);
//            System.out.println("返回res:"+ res);
            JSONObject obj = JSONObject.fromObject(res);
            JSONArray ja = obj.getJSONArray("result");
            List<CarPosition> carPositions = new ArrayList<>();
            if (vclNs != null && vclNs.length() > 0 && ja != null && ja.size() > 0) {
                String[] cars = vclNs.split(",");
                String[] billIdStrs = billIds.split(",");

                CarPosition carPosition = null;
                JSONObject obj2 = null;
                for (int i = 0; i < cars.length; i++) {
                    boolean jaRet = false;
                    carPosition = new CarPosition();

                    for (int j = 0; j < ja.size(); j ++) {
                        obj2 = ja.getJSONObject(j);
                        jaRet = true;
                        if(StringUtil.isBlank(obj2.getString("lon"),obj2.getString("lat"),obj2.getString("utc"))){
                            continue;
                        }
                        if (cars[i].indexOf(obj2.getString("vno")) != -1) {
                            carPosition.setBillId(billIdStrs[i]);
                            jaRet = false;
                            break;
                        }
                    }

//                    System.out.println("cc44444444444444"+obj2.getString("vno")+"=vno="+jaRet+obj2.getString("lon")+"=lon="+obj2.getString("lat")+"=lat="+obj2.getString("utc")+"=utc=");
                    if(jaRet) {
                        continue;
                    }
//                    carPosition.setId(ObjectId.get());
                    carPosition.setVno(obj2.getString("vno"));
//                        carPosition.setOriginalLon(obj2.getString("lon"));
//                        carPosition.setOriginalLat(obj2.getString("lat"));
                    Float lon = Float.valueOf(obj2.getString("lon"))/600000;
                    carPosition.setLon(lon.toString());
                    Float lat = Float.valueOf(obj2.getString("lat"))/600000;

                    carPosition.setLat(lat.toString());
                    carPosition.setVco(obj2.getString("vco"));
                    carPosition.setAdr(obj2.getString("adr"));
                    carPosition.setSpd(obj2.getString("spd"));
                    carPosition.setProvince(obj2.getString("province"));
                    carPosition.setCity(obj2.getString("city"));
                    carPosition.setCountry(obj2.getString("country"));
                    carPosition.setDrc(obj2.getString("drc"));
                    carPosition.setUtc(DateUtil.timeStamp2Date(obj2.getString("utc"), ""));
                    carPosition.setCreateDate(DateUtil.getCurrentDate());
                    carPositions.add(carPosition);
                }
            }
            return carPositions;
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
        }
        return null;
    }
    /** 一
     * 轨迹 所有车辆 但是要钱
     * */
    public static List<CarPosition> vHisTrack24(String vclN, String qryBtm, String qryEtm, String runwithmodel) {
        try {
            setStr(runwithmodel);
            Map<String, String> map = new HashMap<String, String>(3);
            map.put("token", getToken());
            map.put("cid", CID);
            map.put("srt", SRT);
            map.put("vclN", vclN);
            map.put("qryBtm", qryBtm);
            map.put("qryEtm", qryEtm);
            String url = URL+"/save/apis/vHisTrack24";
            DataExchangeService des = new DataExchangeService(5000, 8000);
//            System.out.println("请求地址:"+url);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);
//            System.out.println("vclNs+billIds:"+ vclNs+"   "+billIds);
//            System.out.println("返回res:"+ res);
            JSONObject obj = JSONObject.fromObject(res);
            JSONArray ja = obj.getJSONArray("result");
            List<CarPosition> carPositions = new ArrayList<>();

            if (ja != null && ja.size() > 0) {
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject obj2 = ja.getJSONObject(i);
                    if(StringUtil.isBlank(obj2.getString("lon"),obj2.getString("lat"),obj2.getString("gtm"))){
                        continue;
                    }
                    try {
                        CarPosition carPosition = new CarPosition();
                        Float lon = Float.valueOf(obj2.getString("lon"))/600000;
                        carPosition.setLon(lon.toString());
                        Float lat = Float.valueOf(obj2.getString("lat"))/600000;
                        carPosition.setLat(lat.toString());
                        carPosition.setGtm(obj2.getString("gtm"));
                        carPosition.setSpd(obj2.getString("spd"));
                        carPosition.setMlg(obj2.getString("mlg"));
                        carPosition.setHgt(obj2.getString("hgt"));
//                        carPosition.setUtc(DateUtil.timeStamp2Date(obj2.getString("utc"), ""));
                        carPositions.add(carPosition);
                    } catch (Exception e) {
                    }
                }
            }
            return carPositions;
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
        }
        return null;
    }


    /**
     * 确认是否是入网车辆
     * https://testopen.95155.com/save/apis/checkTruckExist
     * */
    public static String checkTruckExist(String vclN, String runwithmodel) {
        try {
            setStr(runwithmodel);
            Map<String, String> map = new HashMap<String, String>(3);
            map.put("token", getToken());
            map.put("cid", CID);
            map.put("srt", SRT);
            map.put("vclN", vclN);
            String url = URL+"/save/apis/checkTruckExist";
            DataExchangeService des = new DataExchangeService(5000, 8000);
//            System.out.println("请求地址:"+url);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);
//            System.out.println("返回res:"+ res);
            JSONObject obj = JSONObject.fromObject(res);
            String ja = obj.getString("result");

            return ja;
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
        }
        return null;
    }

    /**
     * 先判断是否是入网车辆并返回车机的最后在线时间
     * https://testopen.95155.com/save/apis/checkVehicleExist
     * */
    public static String checkVehicleExist(String vclN, String runwithmodel) {
        try {
            setStr(runwithmodel);
            Map<String, String> map = new HashMap<String, String>(3);
            map.put("token", getToken());
            map.put("cid", CID);
            map.put("srt", SRT);
            map.put("vclN", vclN);
            String url = URL+"/save/apis/checkVehicleExist";
            DataExchangeService des = new DataExchangeService(5000, 8000);
//            System.out.println("请求地址:"+url);
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            String res = des.postHttps(url, map);
//            System.out.println("返回res:"+ res);
            JSONObject obj = JSONObject.fromObject(res);
            String ja = obj.getString("result");

            return DateUtil.formatDate(new Date(new Long(ja)));
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
        }
        return null;
    }

    public static void main(String[] args) {
        String a = "1611890328000";
        System.out.println(DateUtil.formatDate(new Date(new Long(a))));
    }

    private HttpComponentsClientHttpRequestFactory generateHttpRequestFactory()
            throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
        TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());

        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);
        return factory;

    }
}